
#include "comLogManager.h"
#include "taskLuaMgr.h"

#include <map>
using namespace std;

#include <boost/foreach.hpp>


taskLuaMgr::taskLuaMgr(const string &tthome)
:m_L(NULL)
,m_TThome(tthome)
,m_Home("")
,m_Arch("portable")
{
}

taskLuaMgr::taskLuaMgr(const string &tthome,const string &arch)
:m_L(NULL)
,m_TThome(tthome)
,m_Home("")
,m_Arch(arch)
{
}

taskLuaMgr::~taskLuaMgr()
{
	lua_close(m_L);
}
void taskLuaMgr::initEnv(void)
{
	int ret = 0;
	m_Home = m_TThome + "/tt-app/luaScript";
	m_L = lua_open();
	luaL_openlibs(m_L);

	ttcode_log << "m_Home=<" << m_Home << ">" << endl;
	
	string initLuaStr;
	initLuaStr = "package.cpath = package.cpath..\";";
	initLuaStr += m_TThome + "/tt-app/lib/?.dll\"";
	ret = luaL_dostring(m_L,initLuaStr.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	initLuaStr = "package.path = package.path..\";";
	initLuaStr += m_Home + "/?.lua\"";
	ret = luaL_dostring(m_L,initLuaStr.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	ret = luaL_dostring(m_L,"io.output(\"ttcode.lua.log\")");
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	initLuaStr = "require(\"ttbuild_";
	initLuaStr += m_Arch + "\")";
	ret = luaL_dostring(m_L,initLuaStr.c_str());
	ttcode_log << "initLuaStr=<" << initLuaStr << ">" << endl;
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	ret = luaL_dostring(m_L,"require(\"ttbuild_core\")");
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	initLuaStr = "ttbuild.home = \"";
	initLuaStr += m_TThome + "\"";
	ret = luaL_dostring(m_L,initLuaStr.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	ret = luaL_dostring(m_L,"ttbuild.showcmd = false");
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

//	const char *msgError = lua_tostring(m_L, -1);
//	ttcode_log << endl;

//	this->callAction("bc_c","C:/local-TaoTie-dev/src/Root/tt-app/luaScript/main.c",
//		"C:/local-TaoTie-dev/src/Root/tt-app/luaScript/main.c.bc","-O3","cc.log");
}

void taskLuaMgr::callLuaApp(const string &name)
{
	this->initEnv();
	int ret = luaL_dofile(m_L,name.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}
}

void taskLuaMgr::callLuaString(const string &script)
{
	this->initEnv();
	int ret = luaL_dostring(m_L,script.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}
}


int taskLuaMgr::callAction(const string &action,const string &in,const string &out,const string &opt,const string &log,string &runCmd)
{
//	ttcode_log << "action=<" << action << ">" << endl;
//	ttcode_log << "in=<" << in << ">" << endl;
//	ttcode_log << "out=<" << out << ">" << endl;
//	ttcode_log << "opt=<" << opt << ">" << endl;
//	ttcode_log << "log=<" << log << ">" << endl;
	this->initEnv();

	string script("ttbuild:");
	script += action;
	script += "(";

	script += "\"";
	script += in;
	script += "\",";

	script += "\"";
	script += out;
	script += "\",";

	script += "\"";
	script += opt;
	script += "\",";

	script += "\"";
	script += log;
	script += "\"";

	script += ")";

	ttcode_log << "script=<" << script << ">" << endl;
	int ret = luaL_dostring(m_L,script.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;

		ofstream of(log.c_str());
		of << msgError << endl;
		of.close();
	}
	else
	{
		lua_getglobal(m_L,"globalExeRet");
		ret = lua_tointeger(m_L, -1);
		ttcode_log << "ret=<" << ret << ">" << endl;
	}
	lua_getglobal(m_L,"globalExeCmd");
	const char *cmdline = lua_tostring(m_L, -1);
	if(cmdline)
	{
		ttcode_log << "cmdline=<" << cmdline << ">" << endl;
		runCmd = cmdline;
	}
	return ret;
}

int taskLuaMgr::callFile(const string &name,const vector<string> &argv)
{
	this->initEnv();
	string argScript;
	argScript = "arg = { }\n";
	BOOST_FOREACH( string arg, argv )
	{
		argScript += "table.insert(arg,\"";
		argScript += arg;
		argScript += "\")\n";
	}
	ttcode_log << "argScript=<" << argScript << ">" << endl;
	int ret = luaL_dostring(m_L,argScript.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
		return ret;
	}

	string allpath = m_Home + "/" + name;
	ret = luaL_dofile(m_L,allpath.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}
	return ret;
}
